package mybatis.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mybatis.entity.User;
import mybatis.service.UserService;
import mybatis.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
	private UserService service;
	{
		service = applicationContext.getBean("userServiceImpl",UserService.class);
	}
	public void exist(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		System.out.println(username);
		boolean flag = service.exist(username);
		System.out.println(flag);
		try {
			responseJson(response,flag);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void regest(HttpServletRequest request, HttpServletResponse response) {
		String code = UUID.randomUUID().toString().replace("-", "");
		Map<String, String[]> parameterMap = request.getParameterMap();
		User user = new User();
		user.setCode(code);
		try {
			BeanUtils.populate(user, parameterMap);
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(user);
		service.regest(user);
		try {
			response.sendRedirect(request.getContextPath()+"/index.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void active(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String code = request.getParameter("code");
		boolean flag = service.active(code);
		if(flag) {
			response.sendRedirect("/GameHub/NewFile.html");
		}else {
			response.sendRedirect("/GameHub/pages/login.jsp");
		}
	}
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user = service.login(username, password);
		if(user == null) {
			request.setAttribute("msg", "用户名或者密码错误");
			System.out.println(11111);
			request.getRequestDispatcher("/pages/login.jsp").forward(request, response);
		}else {
			int status = user.getStatus();
			if(status == 1) {
				HttpSession session = request.getSession();
				session.setAttribute("user", user);
				response.sendRedirect(request.getContextPath()+"/index.jsp");
			}else {
				request.setAttribute("msg", "邮箱未验证");
				request.getRequestDispatcher("/pages/login.jsp").forward(request, response);
			}
			
		}
	};
	public void exit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.getSession().removeAttribute("user");
		response.sendRedirect(request.getContextPath()+"/index.jsp");
	}
}
